package model.user;

/**
 * This class is used to keep track of a specific users credit
 */

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class UserBank implements Serializable {
	private int balance;
	
	/**
	 * Default constructor which sets the users balance to 1000 credits
	 */
	public UserBank() {
		balance = 1000;
	}
	
	/**
	 * Constructor which takes in a specific number of credits to set to a player
	 */
	public UserBank(int money) {
		balance = money;
	}
	
	//Getters and setters
	
	public int getBalance() {
		return balance;
	}

	public void setBalance(int balance) {
		this.balance = balance;
	}

	/**
	 * Remove a specific amount of credits from the users balance
	 * @param withdrawal amount of credits to be removed
	 * @param username Username to remove credits from
	 */
	public void withdraw(String username, int withdrawal) {
		//Access the database and subtract chipsBrought from the Bank Roll
				try {
					Statement stmt;
					ResultSet rs;
					Class.forName("com.mysql.jdbc.Driver").newInstance();
					String url = "jdbc:mysql://instance10905.db.xeround.com:7414/fcsgroup2"; //Location of Database
					
					//Connect to the Database
					Connection con = DriverManager.getConnection(url,"fcsgroup2", "qwerty123");

					System.out.println("URL: " + url);
					System.out.println("Connection: " + con);
					
					//Get a Statement Object
					stmt = con.createStatement();
					
					String fetchInfo = "SELECT BankRoll from users WHERE Username='"+username+"'";
					rs = stmt.executeQuery(fetchInfo);
					rs.next();
					int newBankRoll = rs.getInt("BankRoll") - withdrawal;
					
					String update = "UPDATE users SET BankRoll="+newBankRoll+" WHERE Username='"+username+"'";
					stmt.executeUpdate(update);
					
					stmt.close();
					rs.close();
				}
				catch(Exception e) {
					e.printStackTrace();
				}
		balance = balance - withdrawal;
	}
	
	/**
	 * Adds a specific amount of credits to the users balance
	 * @param username Username to add credits to
	 * @param deposit amount of credits to be added
	 */
	public void deposit(String username, int deposit) {
		try {
			Statement stmt;
			ResultSet rs;
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			String url = "jdbc:mysql://instance10905.db.xeround.com:7414/fcsgroup2"; //Location of Database
			
			//Connect to the Database
			Connection con = DriverManager.getConnection(url,"fcsgroup2", "qwerty123");

			System.out.println("URL: " + url);
			System.out.println("Connection: " + con);
			
			//Get a Statement Object
			stmt = con.createStatement();
			
			String fetchInfo = "SELECT BankRoll from users WHERE Username='"+username+"'";
			rs = stmt.executeQuery(fetchInfo);
			rs.next();
			int newBankRoll = rs.getInt("BankRoll") + deposit;
			
			String update = "UPDATE users SET BankRoll="+newBankRoll+" WHERE Username='"+username+"'";
			stmt.executeUpdate(update);
			
			stmt.close();
			rs.close();
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		balance = balance + deposit;
	}
}
